
#include<iostream>
#include<vector>
using namespace std;
struct Tree
{
	int val;
	Tree* left;
	Tree* right;
	Tree(int x):val(x),left(nullptr),right(nullptr){}
};
Tree* buildTree(vector<int>& v, int& index)
{
	if (index >= v.size() || v[index] == 0)
	{
		index++;
		return nullptr;
	}
	Tree* root = new Tree(v[index]);
    index++;
	root->left = buildTree(v, index);
	root->right = buildTree(v, index);
	return root;
}
void inorder(Tree* root)//中根遍历
{
	if (root == nullptr)return;
	inorder(root->left);
	cout << root->val << " ";
	inorder(root->right);
}
void Delete(Tree* root)//删除节点
{
	if (root == nullptr)return;
	Delete(root->left);
	Delete(root->right);
	delete root;
}
Tree* findkey(Tree* root, int k)//找到目标节点
{
	if (root == nullptr)return nullptr;
	if (root->val == k)return root;
	Tree* leftresult = findkey(root->left, k);
	if (leftresult != nullptr)return leftresult;
	return findkey(root->right, k);
}
Tree* deleteTree(Tree* root, int k)//最终操作
{
	Tree* keytree = findkey(root, k);
	if (keytree == nullptr)
{
	cout << 0;
	return root;
}
	Delete(keytree);
	if (root->left == keytree) 
	{
		root->left = nullptr;
	}
	else if (root->right == keytree) 
	{
		root->right = nullptr;
	}
	inorder(root);
	return root;
}
int main()
{
	vector<int>v;
	int num;
	while (cin >> num)
	{
		num=num+10;
		v.push_back(num);
		if(cin.peek()=='\n')break;
	}

	int m;
	cin >> m;
	vector<int> k(110);
	for (int i = 0; i < m; i++) 
	{
		cin >> k[i];
	}

	int index = 0;
	Tree* root = buildTree(v, index);

	for (int i = 0; i < m; i++)
	{
		deleteTree(root, k[i]);
		cout << endl;
	}

	return 0;
}